#include("/common/_layout.html")
#@layout()
#define layoutHead()
	<title>用户编辑</title>
#end
#define layoutBody()
	<body>
		<div class="page-content-wrap">
				<form class="layui-form" lay-filter="formedit">
					<div class="layui-tab" style="margin: 0;">
						<ul class="layui-tab-title">
							<li><a href="javascript:window.history.back();">用户列表</a></li>
							<li class="layui-this">用户编辑</li>
						</ul>
						<input type="hidden" name="user_id">
						<div class="layui-tab-content">
							<div class="layui-tab-item"></div>
							<div class="layui-tab-item layui-show">
								<div class="layui-form-item">
									<label class="layui-form-label">登录名：</label>
									<div class="layui-input-block">
										<input type="text" name="login_name" required lay-verify="required" placeholder="请输入登录名" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">登录密码：</label>
									<div class="layui-input-block">
										<input type="password" name="user_pwd" placeholder="不修改，请置空" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">姓名：</label>
									<div class="layui-input-block">
										<input type="text" name="user_name" placeholder="用户真实姓名" autocomplete="off" class="layui-input">
									</div>
								</div>
								<hr>
								<div class="layui-form-item">
									<label class="layui-form-label">应用角色：</label>
									<div class="layui-input-block" id="divapprole">
									</div>
								</div>
								<hr>
								<div class="layui-form-item">
									<label class="layui-form-label">手机：</label>
									<div class="layui-input-block">
										<input type="text" name="user_mobile" placeholder="请输入手机" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">手机认证：</label>
									<div class="layui-input-block">
										<input type="checkbox" name="is_mobile" title="认证" lay-skin="primary">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">邮箱：</label>
									<div class="layui-input-block">
										<input type="text" name="user_email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">邮箱认证：</label>
									<div class="layui-input-block">
										<input type="checkbox" name="is_email" title="认证" lay-skin="primary">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">注册时间：</label>
									<div class="layui-input-block">
										<input type="text" name="reg_time" placeholder="请置空" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">锁定账户：</label>
									<div class="layui-input-block">
										<input type="checkbox" name="is_lock" title="锁定" lay-skin="primary">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">用户状态：</label>
									<div class="layui-input-block">
										<select name="user_state" lay-verify="required">
											<option value="1">启用</option>
											<option value="0">禁用</option>
										</select>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="layui-form-item" style="padding-left: 10px;">
						<div class="layui-input-block">
							<button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
							
						</div>
					</div>
				</form>
		</div>
		<!-- #[[ -->
		<script id="tplapprole" type="text/html">
		{{#  layui.each(d.listapp, function(index, item){ }}
			{{#  if(item.app_state === 1 && item.app_id === 1){ }}
				<div style="border:1px solid #ccc; margin-top: 10px; padding: 10px;">
					<input type="checkbox" name="app_id_{{ item.app_id }}" checked title="{{ item.app_name }}">
					<hr>
					{{#  layui.each(d.listrole, function(index2, item2){ }}
						<input type="checkbox" name="role_id_{{ item.app_id }}_{{ item2.role_id }}" title="{{ item2.role_name }}" lay-skin="primary">
					{{#  }); }}	
				</div>
			{{#  } }} 
		{{#  }); }}	
		</script>
		<!-- ]]# -->
		<script src="/static/js/jquery.formHelp.js" type="text/javascript" charset="utf-8"></script>
		<script>

			layui.use(['form', 'layer', 'laytpl'], function() {
				var form = layui.form();
				var layer = layui.layer;
				var laytpl = layui.laytpl;
				// 表单验证
				form.verify({
					username: function(value, item){ //value：表单的值、item：表单的DOM对象
						if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
							return '用户名不能有特殊字符';
						}
						if(/(^\_)|(\__)|(\_+$)/.test(value)){
							return '用户名首尾不能出现下划线\'_\'';
						}
						if(/^\d+\d+\d$/.test(value)){
							return '用户名不能全为数字';
						}
					}
					,passwd: [
						/^[\S]{6,12}$/
						,'密码必须6到12位，且不能出现空格'
					] 
				});

				//监听提交
				form.on('submit(formDemo)', function(formData){

					sendPost("/api/user/saveUser", formData.field, function(res) {
						layer.alert(res.msg, {
							title: '信息提示'
						}, function(idx){
							if (res.code==200){
								//window.location.href = '/admin/list/user?app_id=#(login_app_id)';
								window.history.back();
							}
							layer.close(idx);
						});
					});
					return false;
				});

				// 页面初始化
				function page_init(){
					var id = getUrlParam('id');
					if (id===null || id === 'undefined' || id<=0){
						$("[name='login_pwd']").attr("lay-verify","passwd").attr("placeholder","请输入密码");
					}
					// 加载初始化编辑数据
					sendGet("/api/user/getUser", {
						id : getUrlParam("id")
					}, function(res) {
						//console.log(res);
						// 初始化 应用列表  角色列表
						var getTpl = document.getElementById('tplapprole').innerHTML;
						laytpl(getTpl).render(res.initdata, function(html){
							document.getElementById('divapprole').innerHTML = html;
							//console.log(html);
						});
						// 初始化编辑数据
						if (res.hasOwnProperty("userinfo")) {
							$("form.layui-form").setForm(res.userinfo);
						}
						if (res.hasOwnProperty("userapp")) {
							$("form.layui-form").setCheckBoxList(res.userapp, 'app_id', 'app_id');
						}
						if (res.hasOwnProperty("userrole")) {
							$("form.layui-form").setCheckBoxList(res.userrole, 'role_id', 'app_id,role_id');
						}
						form.render();
					});
				}

				page_init();
			});
		</script>
	</body>

#end